package com.zhl.media.app.mvp.presenter;

import android.app.Application;
import android.widget.Toast;

import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;
import com.muddzdev.styleabletoast.StyleableToast;
import com.orhanobut.hawk.Hawk;
import com.zhl.media.app.R;
import com.zhl.media.app.greendao.AdJustDao;
import com.zhl.media.app.greendao.ElevatorDao;
import com.zhl.media.app.greendao.MediaPositionDao;
import com.zhl.media.app.greendao.ProjDao;
import com.zhl.media.app.model.AdJust;
import com.zhl.media.app.model.Elevator;
import com.zhl.media.app.model.MediaPosition;
import com.zhl.media.app.mvp.contract.MediaContract;
import com.zhl.media.app.mvp.ui.activity.MediaActivity;
import com.zhl.media.app.utils.EntityManager;
import com.zhl.media.commonsdk.base.BaseRespose;
import com.zhl.media.commonsdk.data.UserUtil;
import com.zhl.media.commonsdk.entity.AdJustMedia;
import com.zhl.media.commonsdk.enumeration.MediaCode;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


@ActivityScope
public class MediaPresenter extends BasePresenter<MediaContract.Model, MediaContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public MediaPresenter(MediaContract.Model model, MediaContract.View rootView) {
        super(model, rootView);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void saveAdjust(String oldMediaId,String des,int adjustStatus){
        UserUtil.setAdjust(true);
        AdJustDao adJustDao = EntityManager.getInstance(mApplication).getAdJustDao();
        AdJust adJust = new AdJust(UUID.randomUUID().toString().replace("-", ""),oldMediaId,"",des,adjustStatus,0);
        adJustDao.insert(adJust);
        StyleableToast.makeText(mApplication, "调整成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        mRootView.onYCres();
    }

    public void updateAdjust(String oldMediaId){
        UserUtil.setAdjust(true);
        AdJustDao adJustDao = EntityManager.getInstance(mApplication).getAdJustDao();
        AdJust adJust = adJustDao.queryBuilder().where(AdJustDao.Properties.OldMediaId.eq(oldMediaId)).unique();
        adJustDao.delete(adJust);
        StyleableToast.makeText(mApplication, "撤销成功", Toast.LENGTH_SHORT, R.style.mytoast).show();
        mRootView.onYCres();
    }

    public void selectAdjustCount(){
        AdJustDao adJustDao = EntityManager.getInstance(mApplication).getAdJustDao();
        mRootView.onDtzCount(adJustDao.queryBuilder().where(AdJustDao.Properties.AdjustStatus.eq(1),AdJustDao.Properties.IsAdjust.eq(0)).list().size());
    }

    public void adjustMediaInfo(MediaCode code,String mediaId) {

        AdJustDao adJustDao = EntityManager.getInstance(mApplication).getAdJustDao();
        ProjDao projDao = EntityManager.getInstance(mApplication).getProjDao();
        ElevatorDao elevatorDao = EntityManager.getInstance(mApplication).getElevatorDao();
        MediaPositionDao mediaPositionDao = EntityManager.getInstance(mApplication).getMediaPositionDao();
        AdJust adJust = new AdJust();
        if(code==MediaCode.WCTZ){
            adJust = adJustDao.queryBuilder().where(AdJustDao.Properties.OldMediaId.eq(mediaId)).unique();
            MediaPosition newMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(adJust.getNewMediaId())).unique();
            AdJustMedia adJustMedia = new AdJustMedia();
            adJustMedia.setProjectName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(newMedia.getProjectId())).unique().getName());
            adJustMedia.setBuildingName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(newMedia.getBuildingId())).unique().getName());
            adJustMedia.setUnitName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(newMedia.getUnitId())).unique().getName());
            Elevator elevator = elevatorDao.queryBuilder().where(ElevatorDao.Properties.Id.eq(newMedia.getElevatorId())).unique();
            adJustMedia.setElevatorName(elevator.getName());
            adJustMedia.setInstallPosition(newMedia.getInstallPosition());
            adJustMedia.setSchemeName(newMedia.getSchemeName());
            adJustMedia.setAlphabetIdentification(newMedia.getAlphabetIdentification());
            adJustMedia.setElevatorPosition(elevator.getElevatorPosition());
            adJustMedia.setSchemeId(newMedia.getSchemeId());
            adJustMedia.setId(adJust.getId());
            mRootView.onAdjustMediaInfo(adJustMedia,code);
        }
        if(code==MediaCode.YTZ){
            adJust = adJustDao.queryBuilder().where(AdJustDao.Properties.NewMediaId.eq(mediaId)).unique();
            MediaPosition oldMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(adJust.getOldMediaId())).unique();
            AdJustMedia adJustMedia = new AdJustMedia();
            adJustMedia.setProjectName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldMedia.getProjectId())).unique().getName());
            adJustMedia.setBuildingName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldMedia.getBuildingId())).unique().getName());
            adJustMedia.setUnitName(projDao.queryBuilder().where(ProjDao.Properties.Id.eq(oldMedia.getUnitId())).unique().getName());
            Elevator elevator = elevatorDao.queryBuilder().where(ElevatorDao.Properties.Id.eq(oldMedia.getElevatorId())).unique();
            adJustMedia.setElevatorName(elevator.getName());
            adJustMedia.setInstallPosition(oldMedia.getInstallPosition());
            adJustMedia.setSchemeName(oldMedia.getSchemeName());
            adJustMedia.setAlphabetIdentification(oldMedia.getAlphabetIdentification());
            adJustMedia.setElevatorPosition(elevator.getElevatorPosition());
            adJustMedia.setSchemeId(oldMedia.getSchemeId());
            adJustMedia.setId(adJust.getId());
            mRootView.onAdjustMediaInfo(adJustMedia,code);
        }
    }

    public void cancelAdjust(MediaCode code, String mediaId) {
        UserUtil.setAdjust(true);
        MediaPositionDao mediaPositionDao = EntityManager.getInstance(mApplication).getMediaPositionDao();
        AdJustDao adJustDao = EntityManager.getInstance(mApplication).getAdJustDao();
        AdJust adJust = new AdJust();
        Map map = new HashMap();
        if (code == MediaCode.WCTZ) {
            adJust = adJustDao.queryBuilder().where(AdJustDao.Properties.OldMediaId.eq(mediaId)).unique();
        }
        if (code == MediaCode.YTZ) {
            adJust = adJustDao.queryBuilder().where(AdJustDao.Properties.NewMediaId.eq(mediaId)).unique();
        }

        MediaPosition newMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(adJust.getNewMediaId())).unique();
        MediaPosition oldMedia = mediaPositionDao.queryBuilder().where(MediaPositionDao.Properties.Id.eq(adJust.getOldMediaId())).unique();

        map.put("mediaId",newMedia.getId());
        map.put("unitId",newMedia.getUnitId());
        map.put("buildingId",newMedia.getBuildingId());
        map.put("projectId",newMedia.getProjectId());
        map.put("oldMediaId",oldMedia.getId());
        map.put("oldUnitId",oldMedia.getUnitId());
        map.put("oldBuildingId",oldMedia.getBuildingId());
        map.put("oldProjectId",oldMedia.getProjectId());

        adJustDao.delete(adJust);
        mRootView.cancelAdjust(map);
        StyleableToast.makeText(mApplication, "撤销成功", Toast.LENGTH_LONG, R.style.mytoast).show();
    }
}


